package com.ffapp.rainmonitorcustom.user;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.BasicBeanStr;
import com.ffapp.rainmonitorcustom.bean.LoginBean;
import com.ffapp.rainmonitorcustom.bean.UpdateBean;
import com.ffapp.rainmonitorcustom.login.LoginActivity;
import com.flyco.dialog.listener.OnBtnClickL;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;
import my.ActivityTool;
import my.DialogUtils;
import my.MySharedPreferences;
import my.SystemParamsUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;


public class UserIndexActivity extends BasisActivity {

    @BindView(R.id.text_version)
    TextView text_version;
    @BindView(R.id.img_update_point)
    ImageView img_update_point;

    @Override
    public void initViews() {
        setContentView(R.layout.user_index_activity);
        super.initViews();
        setTitleBg(R.color.transparent);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setUserView();

        text_version.setText("v"+SystemParamsUtils.getAppVersonName(mContext));

        checkUpdata();
    }


//    void getUserInfo() {
//        RequestParams params = new RequestParams();
////        showProgress();
//        params.put("token", LoginBean.getUserToken());
//        HttpRestClient.post(Constants.URL_USER_INFO, params, new MyHttpListener() {
//                    @Override
//                    public void onSuccess(int httpWhat, Object result) {
//                        LoginBean bean = (LoginBean) result;
//                        LoginBean loginBean = LoginBean.getInstance();
//                        loginBean.copyDataFromUserBean(bean);
//                        setUserView();
//                    }
//
//                    @Override
//                    public void onFailure(int httpWhat, Object result) {
////                        super.onFailure(httpWhat, result);
//                    }
//
//                    @Override
//                    public void onFinish(int httpWhat) {
//                        hideLoading();
//                    }
//                },
//                0, LoginBean.class);
//    }

    @Override
    protected void onResume() {
        super.onResume();
//        getImmersionBar().statusBarDarkFont(true,0.5f);
        getImmersionBar().statusBarDarkFont(true).init();
    }

    private void setUserView() {
        ((TextView)findViewById(R.id.text_name)).setText("欢迎您！"+new MySharedPreferences(mContext).getUser());
//        if (LoginBean.isLogin()) {
//            text_nologin.setVisibility(View.GONE);
//            view_login.setVisibility(View.VISIBLE);
////            UserBean userBean = IXMSDK.getLoginInfoBean();
////            text_name.setText(userBean.getUserName());
////            text_authlevel.setText(userBean.getAuthLevelStr());
//            LoginBean loginBean = LoginBean.getInstance();
//            text_name.setText(loginBean.getNick_name());
//            text_des.setText(loginBean.getMobile());
//            BasisApp.loadImgAsbitmap(mContext, loginBean.getHeadPic(), img_photo, R.drawable.user_icon_default);
////            Glide.with(mContext).load(loginBean.getHeadPic()).asBitmap().placeholder(R.drawable.user_icon_default).into(img_photo);
//        } else {
//            text_nologin.setVisibility(View.VISIBLE);
//            view_login.setVisibility(View.GONE);
//            img_photo.setImageResource(R.drawable.user_icon_default);
//        }
    }


    @OnClick(R.id.view_logout)
    void logout(){
        DialogUtils.DialogTwo(mContext, "", "退出当前账号？", "立即退出", "取消", new OnBtnClickL() {
            @Override
            public void onBtnClick() {
//                LoginActivity.toLoginAllClear(mContext);
                postLogout();
            }
        },null);
    }

    void postLogout() {
        RequestParams params = new RequestParams();
//        params.put("ticket", LoginBean.getInstance().getTicket().getValue());
        params.put("token", LoginBean.getUserToken());
        showLoadingDialog();
        HttpRestClient.post(Constants.URL_LOGIN_LOGOUT, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {

//                        MySharedPreferences mSp = new MySharedPreferences(mContext);
//                        mSp.putIsLogined(false);
//                        mSp.putPassword("");
//                        mSp.putUser("");

                        LoginActivity.toLoginAllClear(mContext);
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
//                        super.onFailure(httpWhat, result);
                        LoginActivity.toLoginAllClear(mContext);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                }, 0,
                BasicBeanStr.class);
    }

    @OnClick(R.id.view_modifypwd)
    void modifypwd(){
        ActivityTool.skipActivity(mContext, ModifyPwdActivity.class);
    }

    @OnClick(R.id.view_update)
    void update(){
        UpdateBean.check(mContext,true);
        img_update_point.setVisibility(View.GONE);
    }


    public  void checkUpdata() {


        RequestParams params = new RequestParams();
//        params.put("app_key", "Android");
        HttpRestClient.post(Constants.URL_UPDATE, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                UpdateBean updateBean = (UpdateBean) result;
               if(updateBean.isNeedUpdate(mContext)){
                   img_update_point.setVisibility(View.VISIBLE);
               }else{
                   img_update_point.setVisibility(View.GONE);
               }
            }

            @Override
            public void onFailure(int httpWhat, Object result) {
//                super.onFailure(httpWhat, result);
                img_update_point.setVisibility(View.GONE);
            }

            @Override
            public void onFinish(int httpWhat) {

            }

        }, 0, UpdateBean.class);

    }

}
